package com.aier.common.service.impl;

import java.util.List;

import javax.annotation.Resource;

import net.eicp.ghostboy.futil.ToPinYin;

import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.stereotype.Component;

import com.aier.admin.entity.SysCity;
import com.aier.admin.entity.mapper.SysCityRowMapper;
import com.aier.common.common.Constant;
import com.aier.common.dao.ICommonDAO;
import com.aier.common.service.ISysCityService;


@Component("sysCityService")
public class SysCityServiceImpl_J implements ISysCityService {

    @Resource(name = "commonDao")
    private ICommonDAO commonDao;

    public void setCommonDao(ICommonDAO commonDao) {
        this.commonDao = commonDao;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<SysCity> searchAllProvince(Short state) {
        if (null == state) {
            return (List<SysCity>) commonDao.list(
                    "select * from sys_city where sub_cid=0",
                    new SysCityRowMapper());
        } else {
            return (List<SysCity>) commonDao.list(
                    "select * from sys_city where sub_cid=0 and cstate=?",
                    new Object[] { state }, new SysCityRowMapper());
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<SysCity> searchCitySub(Integer id, Short state) {
        if (null == state) {
            return (List<SysCity>) commonDao.list(
                    "select * from sys_city where sub_cid=?",
                    new Object[] { id }, new SysCityRowMapper());
        } else {
            return (List<SysCity>) commonDao.list(
                    "select * from sys_city where sub_cid=? and cstate=?",
                    new Object[] { id, state }, new SysCityRowMapper());
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<SysCity> searchAll(Short state) {
        if (null == state) {
            return (List<SysCity>) commonDao.list("select * from sys_city",
                    new SysCityRowMapper());
        } else {
            return (List<SysCity>) commonDao.list(
                    "select * from sys_city where cstate=?",
                    new Object[] { state }, new SysCityRowMapper());
        }
    }

    @Override
    public int updateCityName(Integer cid, String name,short cstate) {

        if (commonDao
                .update("update sys_city set name=?,pin_j=?,pin_a=?,pin_as=?,cstate=? where cid=?",
                new Object[] {
                    name,
                    ToPinYin.getPinYinHeadChar(name),
                    ToPinYin.getPinYin(name),
                    ToPinYin.getPinYin(name, " "),
                    cstate,
                    cid })> 0) {
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public SysCity getCityByCid(Integer cid) {
        return (SysCity) commonDao.listByObject(
                "select * from sys_city where cid=?", new Object[] { cid },
                new SysCityRowMapper());
    }

    @Override
    public int insertCity(SysCity sc, String name, short cstate) {
        SysCity newSc = new SysCity();
        newSc.setCstate(cstate);
        newSc.setCtype(sc.getCtype());
        newSc.setSubCid(sc.getSubCid());
        newSc.setName(name);
        newSc.setPinJ(ToPinYin.getPinYinHeadChar(name));
        newSc.setPinA(ToPinYin.getPinYin(name));
        newSc.setPinAS(ToPinYin.getPinYin(name, " "));    
        try {
            return commonDao.insert(
                    SysCityRowMapper.getPreparedStatementCreator(newSc),
                        new GeneratedKeyHolder()).getKey().intValue();
        } catch (Exception e) {
            return 0;
        }
    }

    @Override
    public int insertSubCity(SysCity sc, String name, short cstate) {
        SysCity newSc = new SysCity();
        newSc.setCstate(cstate);
        newSc.setCtype((short) (sc.getCtype()+Constant.num_10));
        newSc.setSubCid(sc.getCid());
        newSc.setName(name);
        newSc.setPinJ(ToPinYin.getPinYinHeadChar(name));
        newSc.setPinA(ToPinYin.getPinYin(name));
        newSc.setPinAS(ToPinYin.getPinYin(name, " "));    
        try {
            return commonDao.insert(
                    SysCityRowMapper.getPreparedStatementCreator(newSc),
                        new GeneratedKeyHolder()).getKey().intValue();
        } catch (Exception e) {
            return 0;
        }
    }

    @Override
    public int removeCity(Integer cid) {
        removeSubCity(cid);
        return 1;
    }
    
    private void removeSubCity(Integer subid){
        @SuppressWarnings("unchecked")
        List<SysCity> lsc=(List<SysCity>)commonDao.list("select * from sys_city where sub_cid=?",new Object[]{subid},new SysCityRowMapper());
        if(null==lsc||lsc.size()<=0){
            commonDao.update("delete from sys_city where cid=?",new Object[]{subid});
            return;
        }
        for(SysCity sc:lsc){
            removeSubCity(sc.getCid());
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<SysCity> searchCityCtype(Short ctype) {
        return (List<SysCity>)commonDao.list("select * from sys_city where ctype=?",new Object[]{ctype},new SysCityRowMapper());
    }

}
